//
//  EditorContext.cpp
//  Editor
//
//  Created by Jeppe Nielsen on 10/19/14.
//  Copyright (c) 2014 Jeppe Nielsen. All rights reserved.
//

#include "Project.hpp"
#include <vector>
#include <iostream>

void Project::Initialize() {

    

    world.InitializeScripts();
    
    Build();
    
    world.CallMain();
    /*
    for (int i=0; i<10; i++) {
        world.CreateObject();
    }
    
    GameObject* obj = world.CreateObject();
    for (int i=0; i<3; i++) {
        GameObject* child = world.CreateObject();
        child->Parent = obj;
        
        if (i==2) {
            world.CreateObject()->Parent = child;
        }
    }
    */
}

bool Project::Build() {

    std::cout <<"Building path... :"<< path.toStdString() << std::endl;
   
    QDir dir(path);
    
    std::vector<std::string> files;
    
    QDirIterator iterator(dir.absolutePath(), QDirIterator::Subdirectories);
   while (iterator.hasNext()) {
      iterator.next();
      if (!iterator.fileInfo().isDir()) {
         QString filename = iterator.fileName();
         QString path = iterator.filePath();

         if (filename.endsWith(".nano")) {
            files.push_back(path.toStdString());
         }
      }
   }
    
    for (int i=0; i<files.size(); i++) {
        std::cout<<files[i]<<std::endl;
    }
    
    world.Build(files);
    
    return true;
}

void Project::Step() {
    world.Update(1.0f);
}

void Project::Update(float dt) {
    OnUpdate(dt);
}
